package scau.online.web.security;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import scau.cty.base.common.BaseController;
import scau.cty.base.common.IBaseService;
import scau.cty.dictionary.Code;
import scau.cty.entity.User;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/27.
 */
@Api(value = "登录登出控制器",description = "登录登出接口")
@Controller
public class LoginController extends BaseController{

    @Autowired
    private IBaseService<User> userIBaseService;
    @ApiIgnore
    @GetMapping("/login")
    public String goLogin(){
        return "/test/login";
    }

    @ApiOperation(value = "登录")
    @ApiImplicitParams({@ApiImplicitParam(name = "username",value = "登录账号",required = true,paramType = "query"),
        @ApiImplicitParam(name = "password",value = "登录密码",required = true,paramType = "query")
    })
    @PostMapping(value = "/login",produces = "application/json")
    @ResponseBody
    public String doLogin( String username, String password,
                          HttpServletRequest request, HttpServletResponse response){
        Map map = new LinkedHashMap<>();
        logger.info("======进入登陆======");
        SecurityUtils.getSecurityManager().logout(SecurityUtils.getSubject());
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
        System.out.println("我来过这里"+username+password);
        token.setRememberMe(true);
        try {
            subject.login(token);
        } catch (UnknownAccountException ex) {
            logger.catching(ex);
            sendCode(map, Code.USER_NO_EXIST);
            return JSON.toJSONString(map);
        } catch (IncorrectCredentialsException ex) {
            logger.catching(ex);
            sendCode(map, Code.INCORRECT);
            return JSON.toJSONString(map);
//        } catch (Exception e) {
//            logger.error("未知异常");
//            return JSON.toJSONString("未知异常");
        }
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    @ApiOperation(value = "登出")
    @PostMapping(value = "/logout",produces = "application/json")
    @ResponseBody
    public String logout() {
        Map map = new LinkedHashMap<>();
        Subject subject=SecurityUtils.getSubject();
        subject.logout();
        sendCode(map, Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
}
